﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;

namespace Tesla.Graphics.Implementation {
    /// <summary>
    /// Abstract class for concrete subclasses to provide the underlying implementation of <see cref="DepthStencilState"/>.
    /// </summary>
    public abstract class DepthStencilStateImplementation : GraphicsResourceImplementation {
        private bool _isBound;
        private bool _depthEnable;
        private bool _depthWriteEnable;
        private bool _stencilEnable;
        private int _referenceStencil;
        private int _stencilReadMask;
        private int _stencilWriteMask;
        private bool _twoSidedStenciling;
        private ComparisonFunction _ccwStencilCompare;
        private ComparisonFunction _stencilCompare;
        private ComparisonFunction _depthCompare;
        private StencilOperation _ccwStencilDepthFail;
        private StencilOperation _ccwStencilFail;
        private StencilOperation _ccwStencilPass;
        private StencilOperation _stencilDepthFail;
        private StencilOperation _stencilFail;
        private StencilOperation _stencilPass;


        /// <summary>
        /// Gets or protected sets if this DepthStencilStateImplementation has been bound to the pipeline and its underlying objects created.
        /// </summary>
        public bool IsBound {
            get {
                return _isBound;
            }
            protected set {
                _isBound = value;
            }
        }

        /// <summary>
        /// Gets or sets if the depth buffer should be enabled.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public bool DepthEnable {
            get {
                return _depthEnable;
            }
            set {
                ThrowIfBound();
                _depthEnable = value;
            }
        }

        /// <summary>
        /// Gets or sets if the depth buffer should be writable.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public bool DepthWriteEnable {
            get {
                return _depthWriteEnable;
            }
            set {
                ThrowIfBound();
                _depthWriteEnable = value;
            }
        }

        /// <summary>
        /// Gets or sets the depth comparison function for the depth test.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public ComparisonFunction DepthFunction {
            get {
                return _depthCompare;
            }
            set {
                ThrowIfBound();
                _depthCompare = value;
            }
        }

        /// <summary>
        /// Gets or sets if the stencil buffer should be enabled.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public bool StencilEnable {
            get {
                return _stencilEnable;
            }
            set {
                ThrowIfBound();
                _stencilEnable = value;
            }
        }

        /// <summary>
        /// Gets or sets the reference stencil value used for stencil testing.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public int ReferenceStencil {
            get {
                return _referenceStencil;
            }
            set {
                ThrowIfBound();
                _referenceStencil = value;
            }
        }

        /// <summary>
        /// Gets or sets the comparison function used for testing a counterclockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public ComparisonFunction CounterClockwiseStencilFunction {
            get {
                return _ccwStencilCompare;
            }
            set {
                ThrowIfBound();
                _ccwStencilCompare = value;
            }
        }

        /// <summary>
        /// Gets or sets the stencil operation done when the stencil test passes, but the depth test fails
        /// for a counterclockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public StencilOperation CounterClockwiseStencilDepthFail {
            get {
                return _ccwStencilDepthFail;
            }
            set {
                ThrowIfBound();
                _ccwStencilDepthFail = value;
            }
        }

        /// <summary>
        /// Gets or sets the stencil operation done when the stencil test fails for
        /// a counterclockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public StencilOperation CounterClockwiseStencilFail {
            get {
                return _ccwStencilFail;
            }
            set {
                ThrowIfBound();
                _ccwStencilFail = value;
            }
        }

        /// <summary>
        /// Gets or sets the stencil operation done when the stencil test passes for
        /// a counterclockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public StencilOperation CounterClockwiseStencilPass {
            get {
                return _ccwStencilPass;
            }
            set {
                ThrowIfBound();
                _ccwStencilPass = value;
            }
        }

        /// <summary>
        /// Gets or sets the comparison function used for testing a clockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public ComparisonFunction StencilFunction {
            get {
                return _stencilCompare;
            }
            set {
                ThrowIfBound();
                _stencilCompare = value;
            }
        }

        /// <summary>
        /// Gets or sets the stencil operation done when the stencil test passes but
        /// the depth test fails for a clockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public StencilOperation StencilDepthFail {
            get {
                return _stencilDepthFail;
            }
            set {
                ThrowIfBound();
                _stencilDepthFail = value;
            }
        }

        /// <summary>
        /// Gets or sets the stencil operation done when the stencil test fails
        /// for a clockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public StencilOperation StencilFail {
            get {
                return _stencilFail;
            }
            set {
                ThrowIfBound();
                _stencilFail = value;
            }
        }

        /// <summary>
        /// Gets or sets the stencil operation done when the stencil test passes
        /// for a clockwise triangle.
        /// </summary>
        /// <exception cref="System.InvalidOperationException">Thrown if the implementation is bound. Once bound render states are read-only.</exception>
        public StencilOperation StencilPass {
            get {
                return _stencilPass;
            }
            set {
                ThrowIfBound();
                _stencilPass = value;
            }
        }

        /// <summary>
        /// Gets or sets if two sided stenciling should be enabled, that is if counterclockwise
        /// stencil testing/operations should be conducted.
        /// </summary>
        public bool TwoSidedStencilEnable {
            get {
                return _twoSidedStenciling;
            }
            set {
                ThrowIfBound();
                _twoSidedStenciling = value;
            }
        }

        /// <summary>
        /// Gets or sets the value that identifies a portion of the depth-stencil buffer for reading stencil data.
        /// </summary>
        public int StencilReadMask {
            get {
                return _stencilReadMask;
            }
            set {
                ThrowIfBound();
                _stencilReadMask = value;
            }
        }

        /// <summary>
        /// Gets or sets the value that identifies a portion of the depth-stencil buffer for writing stencil data.
        /// </summary>
        public int StencilWriteMask {
            get {
                return _stencilWriteMask;
            }
            set {
                ThrowIfBound();
                _stencilWriteMask = value;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="DepthStencilStateImplementation"/>.
        /// </summary>
        protected DepthStencilStateImplementation() {
            _isBound = false;
        }

        /// <summary>
        /// Binds the implementation. This is called the first time an unbound state is set to the device or manually by the user in order to
        /// create the underlying state ahead of time (best practice). Once called the state properties are read-only.
        /// </summary>
        public abstract void BindDepthStencilState();

        private void ThrowIfBound() {
            if(_isBound) {
                throw new InvalidOperationException("Cannot change render state once it has been bound to the pipeline.");
            }
        }
    }
}
